package com.myblog.controller;


import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.myblog.common.lang.Result;
import com.myblog.entity.Blog;
import com.myblog.service.BlogService;
import com.myblog.util.ShiroUtil;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.apache.shiro.util.Assert;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author mochuy：myblog
 * @since 2021-04-27
 */
@RestController
public class BlogController {
  @Autowired
    BlogService blogService;

  @GetMapping("/blogs")
    public Result list(@RequestParam(defaultValue = "1") Integer currentPage){
      if(currentPage == null || currentPage < 1){ currentPage = 1;}
      Page  page = new Page(currentPage,5);
      IPage pageData = blogService.page(page,new QueryWrapper<Blog>().orderByAsc("created"));
      return Result.succ(pageData);
  }
  @GetMapping("/blog/{id}")
   public Result detail(@PathVariable(name = "id") Long id){
       Blog blog = blogService.getById(id);
       Assert.notNull(blog,"该博客已删除！");
       return Result.succ(blog);
  }

  @RequiresAuthentication
  @PostMapping("/blog/edit")
    public Result edit(@Validated @RequestBody Blog blog){
      Blog temp = null;
      if(blog.getId() != null){
          temp = blogService.getById(blog.getId());
          Assert.isTrue(temp.getUserId() == ShiroUtil.getProfile().getId(),"没有权限编辑");
      }else{
          temp = new Blog();
          temp.setUserId(ShiroUtil.getProfile().getId());
          temp.setCreated(LocalDateTime.now());
          temp.setStatus(0);
      }
      BeanUtil.copyProperties(blog,temp,"id","userId","created","status");
      blogService.saveOrUpdate(temp);
      return Result.succ(200,"操作成功",null);
  }
}
